package com.amt.olcacon.controller.exchange;

import com.amt.olcacon.db.DerbyConfig;
import com.amt.olcacon.vo.GlobalResponse;
import lombok.extern.slf4j.Slf4j;
import org.openlca.core.DataDir;
import org.openlca.core.database.ExchangeDao;
import org.openlca.core.database.ProcessDao;
import org.openlca.core.model.Exchange;
import org.openlca.core.model.Process;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: yangyusheng
 * @Date: 2025/05/28
 */
@Slf4j
@RestController
@RequestMapping("/olca/exchange")
public class ExchangeController {

    @GetMapping("/delete/{id}")
    public GlobalResponse<String> delete(@PathVariable("id") String id) {

        var db = DataDir.get().openDatabase(DerbyConfig.DBNAME);
        new ExchangeDao(db).delete(Long.parseLong(id));
        db.close();

        return GlobalResponse.success();
    }

    @GetMapping("/get/{refId}")
    public GlobalResponse<List<Exchange>> get(@PathVariable("refId") String refId) {

        var db = DataDir.get().openDatabase(DerbyConfig.DBNAME);
        var dao = new ProcessDao(db);

        Process process = dao.getForRefId(refId);
        db.close();
        List<Exchange> exchange = process.exchanges;
        exchange.forEach(i -> {
            if (i.flow != null) {
                i.flow.category = null;
                i.flow.referenceFlowProperty = null;
                i.flow.flowPropertyFactors.clear();
            }
            i.flowPropertyFactor = null;
            i.currency = null;
            i.location = null;
            i.uncertainty = null;

        });
        return GlobalResponse.of(exchange);
    }

}
